package service

import (
	"cangqiong-takeout/src/common"
	"cangqiong-takeout/src/model"
	"cangqiong-takeout/src/utils"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func GetCategoryMsg(c *gin.Context) {
	name := c.Query("name")
	pValue := c.Query("page")
	szieValue := c.Query("pageSize")
	types := c.Query("type")
	page, err := strconv.Atoi(pValue)
	pageSize, err := strconv.Atoi(szieValue)

	if err != nil {
		utils.ResponseBad(c, common.FormatError, nil)
		return
	}
	offset := (page - 1) * pageSize
	if name != "" {
		category, cnt, err := searchCategory(name, types, pageSize, offset)
		if err != nil {
			utils.ResponseBad(c, common.FindError, nil)
			return
		}
		utils.ResponseSuccess(c, common.FindSuccess, gin.H{
			"total":   cnt,
			"records": category,
		})
		return
	}
	var (
		cnt  int64
		cate []*model.Category[int]
	)
	if err := utils.MysqlDB.Table("category").Count(&cnt).Order("sort ASC").Limit(pageSize).Offset(offset).Find(&cate).Error; err != nil {
		utils.ResponseBad(c, common.FindError, nil)
		return
	}
	for i := 0; i < len(cate); i++ {
		cate[i].SetStringToUpdateTime()
		cate[i].SetStringToCreateTime()
	}

	utils.ResponseSuccess(c, common.FindSuccess, gin.H{
		"total":   cnt,
		"records": cate,
	})
}
func searchCategory(name, types string, size, offset int) ([]*model.Category[int], int64, error) {
	var (
		cnt int64
	)
	var cate []*model.Category[int]
	if err := utils.MysqlDB.Table("category").Where("name like ? AND type like ?", "%"+name+"%", "%"+types+"%").Count(&cnt).Order("sort ASC").Limit(size).Offset(offset).Find(&cate).Error; err != nil {
		return nil, 0, err
	}
	for i := 0; i < len(cate); i++ {
		cate[i].SetStringToUpdateTime()
		cate[i].SetStringToCreateTime()
	}
	return cate, cnt, nil
}

func GetCategoryList(c *gin.Context) {
	types := c.Query("type")
	typeNumber, _ := strconv.Atoi(types)
	var cate []*model.Category[int]
	if err := utils.MysqlDB.Table("category").Where("type = ?", typeNumber).Find(&cate).Error; err != nil {
		utils.ResponseBad(c, common.FindError, nil)
		return
	}
	for _, category := range cate {
		category.SetStringToUpdateTime()
		category.SetStringToCreateTime()
	}
	utils.ResponseSuccess(c, common.FindSuccess, cate)
}

func AddCategory(c *gin.Context) {
	var cate model.Category[string]
	if err := c.ShouldBindJSON(&cate); err != nil {
		utils.ResponseBad(c, common.AddError, nil)
		return
	}
	var cnt int64
	utils.MysqlDB.Table("category").Where("name = ?", cate.Name).Count(&cnt)
	if cnt > 0 {
		utils.ResponseBad(c, common.IsExistence, nil)
		return
	}
	cate.CreateTime = time.Now()
	cate.UpdateTime = time.Now()
	userid := c.GetInt("userid")
	cate.CreateUser = userid
	cate.UpdateUser = userid
	cate.Status = common.Enable
	utils.MysqlDB.Table("category").Create(&cate)
	utils.ResponseSuccess(c, common.AddSuccess, nil)
}

func EditCategory(c *gin.Context) {
	var cate model.Category[string]
	if err := c.ShouldBindJSON(&cate); err != nil {
		utils.ResponseBad(c, common.ModifyError, nil)
		return
	}
	if err := utils.MysqlDB.Table("category").Where("id = ?", cate.ID).Updates(&cate).Error; err != nil {
		utils.ResponseBad(c, common.ModifyError, nil)
		return
	}
	utils.ResponseSuccess(c, common.ModifySuccess, nil)
}

func SetCategoryStatus(c *gin.Context) {
	status := c.Param("status")
	ids := c.Query("id")
	if err := utils.MysqlDB.Table("category").Where("id=?", ids).Update("status", status).Error; err != nil {
		utils.ResponseBad(c, common.ModifyError, nil)
		return
	}
	utils.ResponseSuccess(c, common.ModifySuccess, nil)
}

func DeleteCategory(c *gin.Context) {
	ids := c.Query("id")
	var (
		cate    model.Category[int]
		idCount int64
	)
	utils.MysqlDB.Table("dish").Where("category_id=?", ids).Count(&idCount)
	if idCount > 0 {
		utils.ResponseBad(c, "有关联菜品不能删除", nil)
		return
	}
	utils.MysqlDB.Table("setmeal").Where("category_id=?", ids).Count(&idCount)
	if idCount > 0 {
		utils.ResponseBad(c, "有关联套餐不能删除", nil)
		return
	}
	if err := utils.MysqlDB.Table("category").Where("id=?", ids).First(&cate).Delete(&cate).Error; err != nil {
		utils.ResponseBad(c, common.DeleteError, nil)
		return
	}
	utils.ResponseSuccess(c, common.DeleteSuccess, nil)
}
